<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/9
 * Time: 18:53
 */

namespace app\flower\controller;
use cmf\controller\AdminBaseController;
use think\Db;
use app\flower\model\FlowerFlowertypeModel;
use app\flower\service\CommonService;
use think\Request;
use app\flower\model\FlowerFlowerinfoModel;
class AdminFlowerController extends AdminBaseController
{
    /**
     * @return 花卉类型页面
     */
     public function type(){
            $flowertype = new FlowerFlowertypeModel();
            $result = $flowertype->paginate(4);

            $count = Db::name('flowerFlowertype')->count();
            if($count>0){
               $this->assign('flowers',$result);
            }else{
                $this->assign('flowers',array());
            }
            $this->assign('page',$result->render());
            return $this->fetch('type');
     }

    /**
     *花卉类型添加页面
     */
    public function typeadd(){


        return $this->fetch('add');
    }

    /**
     * 花卉类型修改页面
     */
    public function typeedit(){
        $flowertype = new FlowerFlowertypeModel();
        $id = input('id');
        $result = $flowertype->get($id);
        if(empty($result['image'])){
            $image = "__TMPL__/public/assets/images/default-thumbnail.png";
        }else{
            if($_SERVER['HTTP_HOST']=='localhost'){
                $image = cmf_get_image_url($result['image']);
            }else{
                //$image = 'http://'.$_SERVER['HTTP_HOST'].'/'.cmf_get_image_url($result['image']);
                $image = cmf_get_image_url($result['image']);
            }
        }
        $image2 = $this->commonimage($result['image2']);
        $homepage = $this->commonimage($result['homepage']);
        $homepagetitile = $this->commonimage($result['homepagetitile']);
        $flowertitle = $this->commonimage($result['flowertitle']);
        $this->assign('flowertypes',$result);
        $this->assign('image',$image);
        $this->assign('image2',$image2);
        $this->assign('homepage',$homepage);
        $this->assign('homepagetitile',$homepagetitile);
        $this->assign('flowertitle',$flowertitle);
        return $this->fetch('edit');
    }
    public function commonimage($image){
        if(empty($image)){
            $image = "__TMPL__/public/assets/images/default-thumbnail.png";
        }else{
            if($_SERVER['HTTP_HOST']=='localhost'){
                $image = cmf_get_image_url($image);
            }else{
                $image = cmf_get_image_url($image);
            }
        }
        return $image;
    }
    /**
     * 花卉类型添加
     */
    public function add(){
        $flowertype = input('flowerType');
        $image = input('more.thumbnail');
        $image2 = input('image2.thumbnail');
        $homepage = input('homepage.thumbnail');
        $homepagetitile = input('homepagetitile.thumbnail');
        $flowertitle= input('flowertitle.thumbnail');
        $description = input('description');

        $knows = input('flowers.post_content');
        $knows = empty($knows)?'':$knows;
        $service = input('service.post_content');
        $service = empty($service)?'':$service;
        $goodtype = input('goodtype');
        $bottomprice = input('bottomprice');
        if(empty($flowertype)){
          $this->error('请填写花卉类型后再提交');
        }else{
           $result = Db::name('flowerFlowertype')->insert(array('name'=>$flowertype,'knows'=>$knows,'service'=>$service,'image'=>$image,'dottime'=>time(),'image2'=>$image2,'homepage'=>$homepage,'homepagetitile'=>$homepagetitile,'flowertitle'=>$flowertitle,'description'=>$description,'goodtype'=>$goodtype,'bottomprice'=>$bottomprice));
            if($result>0){
               $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }

    /**
     * 花卉类型修改
     */
    public function edit(){
        $flowertype = new FlowerFlowertypeModel();
        $image = input('more.thumbnail');
        $image2 = input('image2.thumbnail');
        $homepage = input('homepage.thumbnail');
        $homepagetitile = input('homepagetitile.thumbnail');
        $flowertitle= input('flowertitle.thumbnail');
        $description = input('description');
        $knows = input('flowers.post_content');
        $knows = empty($knows)?'':$knows;
        $service = input('service.post_content');
        $service = empty($service)?'':$service;
        $id = input('id');
        $flowertypes = input('flowerType');
        $goodtype = input('goodtype');
        $bottomprice = input('bottomprice');
        $result = $flowertype->where(array('id'=>$id))->update(array('name'=>$flowertypes,'knows'=>$knows,'service'=>$service,'image'=>$image,'dottime'=>time(),'image2'=>$image2,'homepage'=>$homepage,'homepagetitile'=>$homepagetitile,'flowertitle'=>$flowertitle,'description'=>$description,'goodtype'=>$goodtype,'bottomprice'=>$bottomprice));
        if($result>0){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    /**
     *花卉类型删除
     */
   /* public function delete(){
        $id = input('id');
        $flowertype = new FlowerFlowertypeModel();
        $res = $flowertype->get($id);
        if(!empty($res['image'])){
            $service = new CommonService();
            $data=$service->filesdelete('./upload/'.$res['image']);//删除图片部分
        }
        if(!empty($res['image2'])){
            $service = new CommonService();
            $data=$service->filesdelete('./upload/'.$res['image2']);//删除图片部分
        }
        if(!empty($res['homepage'])){
            $service = new CommonService();
            $data=$service->filesdelete('./upload/'.$res['homepage']);//删除图片部分
        }
        $result =$flowertype->where(array('id'=>$id))->delete();
        if($result>0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }*/
    /**
     *花卉类型删除
     */
    public function delete(){
        $id = input('id');
        $flowertype = new FlowerFlowertypeModel();
        $res = $flowertype->get($id);
        $status = !$res['status'];
        $result =$flowertype->where(array('id'=>$id))->update(array('status'=>$status));
        if($result>0){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    //=========================================花卉信息管理========================================================
    /**
     * 花卉信息添加页面
     */
    public function flowersadd(){
        //flowersadd
        $flowertype = new FlowerFlowertypeModel();
        $result = $flowertype->all();
        $this->assign('flowertypes',$result);
        return $this->fetch('floweradd');

    }

    /**
     * 花卉信息添加
     */
    public function addflower(){
        $flowerinfo =  new FlowerFlowerinfoModel();
        $fileds = $this->request->param();
      //print_r($fileds);exit;
        $data = [];
        foreach($fileds as $key => $value){
            if(gettype($value) == 'string'){
                $data[$key] = isset($value)?trim($value):'';
            }else{
                $data[$key] = isset($value)?$value:'';
            }
        }
       if(empty($data['imgs'])){
          $this->error('商品图片必填');
        }
        $data['imgs'] = addslashes(json_encode($data['imgs']));
        if(empty($data['details'])){
          $this->error('商品详情必填');
        }
        $data['details'] = htmlspecialchars_decode($data['details']);
        $data['dottime'] = time();
        unset($data['photo_names']);//删除数组中的摸一个元素
        $result = $flowerinfo->insert($data);
        if(!empty($result)){
            $this->success('提交成功！');
        }else{
            $this->error('提交失败！');
        }
    }

    /**
     * 鲜花列表展示页面
     */
    public function flowerindex(){
        $flowerinfo =  new FlowerFlowerinfoModel();
        $result = $flowerinfo->paginate(3);
        $flowertype = new FlowerFlowertypeModel();
        $res = $flowertype->all();

        $page = $result->render();
        $this->assign('flowers',$result);
        $this->assign('flowertypes',$res);
        $this->assign('page',$page);
        return $this->fetch('index');
    }

    /**
     * 设置是否热销
     */
    public function ishot(){
        $id = input('id');
        $flowerinfo =  new FlowerFlowerinfoModel();
        $res = $flowerinfo->get($id);
        $status = ($res['status']=='热销'?0:1);
        $result = $flowerinfo->where(array('id'=>$id))->update(array('status'=>$status));
        if($result>0){
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }

    /**
     * 修改花卉的信息页面
     */
    public function floweredit(){
        $id = input('id');
        $flower = new FlowerFlowerinfoModel();
        $res = $flower->get($id);
        $flowertype = new FlowerFlowertypeModel();
        $types = $flowertype->all();
        $this->assign('flowertypes',$types);
        $this->assign('flower',$res);
       // print_r($res['typeid']);exit;
        return $this->fetch('floweredit');
    }

    /**
     * 修改花卉的信息
     */
    public function editflower(){
        $flowerinfo =  new FlowerFlowerinfoModel();
        $id = input('id');
        $result = $flowerinfo->get($id);
        $service = new CommonService();
       // $data=$service->filesdelete('./upload/'.$result['coverimg']);//删除图片部分
       /* foreach($result['imgs'] as $k =>$v){
            $data=$service->filesdelete('./upload/'.$v);//删除图片部分
        }*/
        $fileds = $this->request->param();
        $data = [];
        foreach($fileds as $key => $value){
            if(gettype($value) == 'string'){
                $data[$key] = isset($value)?trim($value):'';
            }else{
                $data[$key] = isset($value)?$value:'';
            }
        }
        $data['imgs'] = addslashes(json_encode($data['imgs']));//$img = stripslashes($img);   $img = json_decode($img);

        $data['details'] = htmlspecialchars_decode($data['details']);
        $data['dottime'] = time();
        unset($data['photo_names']);//删除数组中的摸一个元素
        $result = $flowerinfo->where('id',$id)->update($data);

        if($result>=1){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     *删除花卉
     */
    public function deleteflower(){
        $id = input('id');
        $flowerinfo =  new FlowerFlowerinfoModel();
        $result = $flowerinfo->get($id);
        $service = new CommonService();
        $data=$service->filesdelete('./upload/'.$result['coverimg']);//删除封面图片部分
        foreach($result['imgs'] as $k =>$v){
            $data=$service->filesdelete('./upload/'.$v);//删除产品展示图片部分
        }
        $result = $flowerinfo->where('id',$id)->delete();
        if($result>0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     *筛选鲜花的信息
     */
    public function search(){
        $flowerinfo =  new FlowerFlowerinfoModel();

        $type = input('flowertype');//鲜花的类型
        $sql=$flowerinfo;
        if($type!='all'){
            $sql = $sql->where('typeid',$type);
        }
        $start_time = input('start_time');//开始时间
        $end_time = input('end_time');//结束时间
        if($start_time!=null&&$end_time!=null){
            $sql = $sql->where('dottime','>=',strtotime($start_time))->where('dottime','<=',strtotime($end_time));
        }else if($start_time!=null&&$end_time==null){
            $this->error('请选择结束时间');
        }else if($start_time==null&&$end_time!=null){
            $this->error('请选择开始时间');
        }
        $keyword = input('keyword');//关键词
        if($keyword!=null){
            $sql = $sql->where('name','like','%'.$keyword.'%');
        }
        $result = $sql->paginate(8);
        //echo $sql->getLastSql();exit;
        $page = $result->render();
        $flowertype = new FlowerFlowertypeModel();
        $res = $flowertype->all();
        $this->assign('flowers',$result);
        $this->assign('flowertypes',$res);
        $this->assign('page',$page);
        return $this->fetch('index');
    }

}